package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;

import com.mysema.query.BooleanBuilder;

import java.util.List;

@Service
@Transactional
public class AftersaleRecordWorktimeService {

	@Autowired
	private AftersaleRecordWorktimeDao aftersaleRecordWorktimeDao;

	private QAftersaleRecordWorktime $ = QAftersaleRecordWorktime.aftersaleRecordWorktime;

	@Autowired
	public void setAftersaleRecordWorktimeDao(
			AftersaleRecordWorktimeDao aftersaleRecordWorktimeDao) {
		this.aftersaleRecordWorktimeDao = aftersaleRecordWorktimeDao;
	}

	public List<AftersaleRecordWorktime> findAll() {
		return ListUtil.toList(aftersaleRecordWorktimeDao.findAll());
	}

	public List<AftersaleRecordWorktime> findAll(Long aftersaleRecordId,
			Long personId, String remark, String orderByAttribute) {
		BooleanBuilder condition = new BooleanBuilder();

		if (IdUtil.isNotEmpty(aftersaleRecordId)) {
			condition.and($.aftersaleRecord.id.eq(aftersaleRecordId));
		}

		if (IdUtil.isNotEmpty(personId)) {
			condition.and($.person.id.eq(personId));
		}

		if (StringUtil.isNotEmpty(remark)) {
			condition.and($.remark.contains(remark));
		}

		return ListUtil.toList(aftersaleRecordWorktimeDao.findAll(condition));
	}

	public Page<AftersaleRecordWorktime> findAll(Long aftersaleRecordId,
			Long personId, String remark, int pageNumber, int pageSize,
			String sortAttribute, String orderDirection) {
		BooleanBuilder condition = new BooleanBuilder();

		if (IdUtil.isNotEmpty(aftersaleRecordId)) {
			condition.and($.aftersaleRecord.id.eq(aftersaleRecordId));
		}
		if (IdUtil.isNotEmpty(personId)) {
			condition.and($.person.id.eq(personId));
		}
		if (StringUtil.isNotEmpty(remark)) {
			condition.and($.remark.contains(remark));
		}

		Sort sortObject = null;
		if (StringUtil.isEmpty(sortAttribute)) {
			sortObject = new Sort(Direction.DESC, "id");
		} else {
			sortObject = new Sort(Direction.fromStringOrNull(orderDirection),
					sortAttribute);
		}

		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize,
				sortObject);
		return aftersaleRecordWorktimeDao.findAll(condition, pageRequest);
	}

	public AftersaleRecordWorktime getAftersaleRecordWorktime(Long id) {
		return aftersaleRecordWorktimeDao.getOne(id);
	}

	public void deleteAftersaleRecordWorktime(Long id) {
		aftersaleRecordWorktimeDao.delete(id);
	}

	public void saveAftersaleRecordWorktime(
			AftersaleRecordWorktime aftersaleRecordWorktime) {
		aftersaleRecordWorktimeDao.save(aftersaleRecordWorktime);
	}

}
